/*
 * 文件名：WSUtil.java
 * 创建改人： wangshi
 * 修改时间：2011-10-26
 */
package com.ws.base.common.util;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 工具类
 * 
 * @author wangshi
 */
public final class WSUtil {

    /**
     * SimpleDateFormat (yyyy-MM-dd)
     * 
     * @return SimpleDateFormat
     */
    public static SimpleDateFormat sDateFmt() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format;
    }

    /**
     * 是否为空
     * 
     * @param str
     *            字符串
     * @return boolean true：为空，false：非空
     */
    public static boolean isEmpty(String str) {
        return null == str || "".equals(str);
    }

    /**
     * 是否为空
     * 
     * @param map
     *            Map
     * @return boolean true：为空，false：非空
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return null == map || map.isEmpty();
    }

    /**
     * 是否为空
     * 
     * @param list
     *            List
     * @return boolean true：为空，false：非空
     */
    public static boolean isEmpty(List<?> list) {
        return null == list || list.isEmpty();
    }

    /**
     * 是否为空
     * 
     * @param strs
     *            strs
     * @return boolean true：为空，false：非空
     */
    public static boolean isEmpty(String[] strs) {
        return null == strs || strs.length == 0;
    }

    /**
     * SimpleDateFormat
     * 
     * @param type
     *            日期格式
     * @return SimpleDateFormat
     */
    public static SimpleDateFormat sDateFmt(String type) {
        SimpleDateFormat format = new SimpleDateFormat(type);
        return format;
    }

    /**
     * 将map的key统一为小写字母
     * 
     * @param map
     *            void
     */
    public static void mapKeytoLowerCase(Map<String, Object> map) {
        Map<String, Object> temp = new HashMap<String, Object>();
        for (Entry<String, ?> e : map.entrySet()) {
            temp.put(e.getKey().toLowerCase(), e.getValue());
        }
        map.clear();
        map.putAll(temp);
    }

    /**
     * 将map的key统一为小写字母
     * 
     * @param map
     *            void
     */
    public static void MapKeytoLowerCase(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            mapKeytoLowerCase(map);
        }
    }

    /**
     * 日期比较
     * 
     * @param firstDate
     *            第一个日期
     * @param secondDate
     *            第二个日期
     * @return 大于:1, 小于:-1, 等于:0
     * @throws ParseException
     */
    public static int dateCompare(String firstDate, String secondDate)
            throws ParseException {
        int rs = 9;
        long tDate = sDateFmt().parse(firstDate).getTime()
                - sDateFmt().parse(secondDate).getTime();
        if (tDate > 0) {
            rs = 1;
        }
        if (tDate < 0) {
            rs = -1;
        }
        if (tDate == 0) {
            rs = 0;
        }
        return rs;
    }

    /**
     * List排序 (对int、Date类型排序)
     * 
     * @param <T>
     * @param isAsc
     *            是否降序
     * @param property
     *            排序属性
     * @param list
     *            排序对象
     * @return result
     */
    public static void listSort(String property, List<?> list, boolean isAsc) {

        Collections.sort(list, new WSComparator(property, isAsc));
    }

    /**
     * remove empty value
     * 
     * @param map
     * @return
     */
    public static Map<String, Object> removeEmpty(Map<String, Object> map) {
        Map<String, Object> rs = new HashMap<String, Object>();

        for (Entry<String, Object> e : map.entrySet()) {
            if (null == e.getValue() || isEmpty(e.getValue().toString())) {
                // map.remove(e);
            } else {
                rs.put(e.getKey(), e.getValue());
            }
        }
        return rs;
    }

    public static String toStr(Object obj) {
        return obj == null ? null : obj.toString();
    }

    /**
     * 获取参数日期的一整月日期
     * 
     * @param date
     * @return
     */
    public static List<Date> getAllTheDateOftheMonth(Date date) {
        List<Date> list = new ArrayList<Date>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);

        int month = cal.get(Calendar.MONTH);
        while (cal.get(Calendar.MONTH) == month) {
            list.add(cal.getTime());
            cal.add(Calendar.DATE, 1);
        }
        return list;
    }
    
    /**
     * close Closeable
     * @param close IO stream
     * @throws IOException
     */
    public static void close(Closeable close) throws IOException {
        if (null != close) {
            close.close();
        }
    }

}

class WSComparator implements Comparator<Object> {

    private String type;

    private boolean isAsc;

    public WSComparator(String type, boolean isAsc) {
        this.type = type;
        this.isAsc = isAsc;
    }

    public int compare(Object o1, Object o2) {
        int flag = 9;

        try {
            Object obj1 = o1.getClass().getMethod(getType(), new Class[] {})
                    .invoke(o1);
            Object obj2 = o2.getClass().getMethod(getType(), new Class[] {})
                    .invoke(o2);
            flag = dateType(obj1, obj2);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return flag;
    }

    private int dateType(Object argObj1, Object argObj2) {
        int flag = 9;
        if (argObj1 instanceof Integer || argObj1 instanceof Long
                || argObj1 instanceof Double || argObj1 instanceof Short) {
            double a = Double.valueOf(argObj1.toString());
            double b = Double.valueOf(argObj2.toString());
            double _flag = (getIsAsc() ? (b - a) : (a - b));
            if (_flag > 0) {
                flag = 1;
            }
            if (_flag < 0) {
                flag = -1;
            }
        }
        if (argObj1 instanceof Date) {
            long a = 0;
            long b = 0;
            a = ((Date) argObj1).getTime();
            b = ((Date) argObj2).getTime();

            long rs = getIsAsc() ? (b - a) : (a - b);
            if (rs > 0) {
                flag = 1;
            }
            if (rs < 0) {
                flag = -1;
            }
        }
        if (argObj1 instanceof String) {
            int a = argObj1.hashCode();
            int b = argObj2.hashCode();
            flag = getIsAsc() ? (b - a) : (a - b);
            if (flag > 0) {
                flag = 1;
            }
            if (flag < 0) {
                flag = -1;
            }
        }
        return flag;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean getIsAsc() {
        return isAsc;
    }

    public void setIsAsc(boolean isAsc) {
        this.isAsc = isAsc;
    }

}
